//引入db
const db = require("./db.js");
const http = require("http");
const dayjs = require("dayjs");
console.log(db.news());

// 创建服务
const server = http.createServer();
// 设置端口号 0~65535之内任意数字
server.listen(9728, () => {
  console.log("my server start work");
});
// 处理请求和响应
let data = {};
server.on("request", (request, response) => {
  const { method, url } = request;
  if (method.toLowerCase() === "get") {
    if (url === "/news") {
      data = db.news();
      data.forEach((el) => {
        el.publish_time = dayjs(el.publish_time).format("YYYY-MM-DD HH:mm:ss");
      });
    } else if (url === "/api/students") {
      data = db.students();
    }else if(url ==='/api/lucystar'){
        data = db.lucyStudent()
        console.log(data);
    }
  }

  // 响应
  response.setHeader("Content-Type", "application/json;charset=utf-8");
  //跨域
  response.setHeader("Access-Control-Allow-Origin", "*");
  //返回响应
  response.end(JSON.stringify(data));
});
